package pers.csw.lookBook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.csw.lookBook.entity.GiveLike;
import pers.csw.lookBook.mapper.GiveLikeMapper;
import pers.csw.lookBook.service.GiveLikeService;
import pers.csw.lookBook.service.RedisService;
import pers.csw.lookBook.utils.RedisKeyUtils;
import pers.csw.lookBook.vo.GiveLikeVo;

import java.util.Date;
import java.util.List;

@Service
public class GiveLikeServiceImpl extends ServiceImpl<GiveLikeMapper, GiveLike> implements GiveLikeService {
    @Autowired
    GiveLikeMapper giveLikeMapper;

    @Autowired
    RedisService redisService;

    @Autowired
    StringRedisTemplate redisTemplate;


    @Override
    @Transactional
    public int saveGiveLike(GiveLike giveLike) {
        return giveLikeMapper.insert(giveLike);
    }

    @Override
    @Transactional
    public boolean saveList(List<GiveLike> giveLikes) {
        return saveBatch(giveLikes);

    }

    @Override
    public GiveLike getGiveLike(Integer userid, Integer pId) {
        QueryWrapper<GiveLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",userid).eq("p_id",pId);
        return giveLikeMapper.selectOne(queryWrapper);
    }

    /**
     * 将Redis里的点赞数据存入数据库中
     */
    @Override
    public void transLikedFromRedis2DB() {
        List<GiveLike> likedDataFromRedis = redisService.getLikedDataFromRedis();
        for (GiveLike likedDataFromRed : likedDataFromRedis) {
            if (getGiveLike(likedDataFromRed.getUId(),likedDataFromRed.getPId())==null){
                System.out.println(123);
                likedDataFromRed.setCreateTime(new Date());
                save(likedDataFromRed);
            }else{
                System.out.println(321);
                likedDataFromRed.setUpdateTime(new Date());
                updateById(likedDataFromRed);
            }
        }
    }

    @Override
    public int getGiveLikeListNum(Integer pId) {
        QueryWrapper<GiveLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_id",pId).eq("statue",1);
        return giveLikeMapper.selectCount(queryWrapper);
    }

    @Override
    public Boolean isUserGiveLike(Integer userId, Integer pId) {
        String likedKey = RedisKeyUtils.getLikedKey(userId, pId);
        String s = redisTemplate.opsForValue().get(likedKey);
        if(s!=null){
            return Integer.parseInt(s) == 1;
        }else{
            GiveLike giveLike = getGiveLike(userId, pId);
            return giveLike != null && giveLike.getStatue()==1;
        }
    }

    @Override
    public List<GiveLikeVo> getUserLike(Integer userId) {
        return this.baseMapper.getAllByUIdGiveLikeVoList(userId);
    }
}
